package domain;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@Entity
@Table(name = "incomes")
@NamedQueries({
	@NamedQuery(name = "IncomeRecord.findAll", query = "SELECT r FROM IncomeRecord r")
})
public class IncomeRecord implements Serializable {

	private static final long serialVersionUID = 1L;
	@Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name = "id", nullable = false)
	private int id;
	@ManyToOne()
	@JoinColumn(name = "user_id", nullable = false)
	private User user;
	@ManyToOne()
	@JoinColumn(name = "book_id", nullable = false)
	private Book book;
	@Temporal(value = TemporalType.DATE)
	@Column(name = "real_return_date")
	private Date realReturnDate;
	@Column(name = "delay_fine")
	private double delayFine;
	@Column(name = "damage_fine")
	private double damageFine;
	
	public IncomeRecord() {}
	
	public IncomeRecord(int id, User user, Book book, Date realReturnDate,
			double delayFine, double damageFine) {
		super();
		this.id = id;
		this.user = user;
		this.book = book;
		this.realReturnDate = realReturnDate;
		this.delayFine = delayFine;
		this.damageFine = damageFine;
	}

	public IncomeRecord(User user, Book book, Date realReturnDate,
			double delayFine, double damageFine) {
		this.user = user;
		this.book = book;
		this.realReturnDate = realReturnDate;
		this.delayFine = delayFine;
		this.damageFine = damageFine;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public Book getBook() {
		return book;
	}

	public void setBook(Book book) {
		this.book = book;
	}

	public Date getRealReturnDate() {
		return realReturnDate;
	}

	public void setRealReturnDate(Date realReturnDate) {
		this.realReturnDate = realReturnDate;
	}

	public double getDelayFine() {
		return delayFine;
	}

	public void setDelayFine(double delayFine) {
		this.delayFine = delayFine;
	}

	public double getDamageFine() {
		return damageFine;
	}

	public void setDamageFine(double damageFine) {
		this.damageFine = damageFine;
	}

	@Override
	public String toString() {
		return "IncomeRecord [id=" + id + ", user=" + user + ", book=" + book
				+ ", realReturnDate=" + realReturnDate + ", delayFine="
				+ delayFine + ", damageFine=" + damageFine + "]";
	}
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((book == null) ? 0 : book.hashCode());
		long temp;
		temp = Double.doubleToLongBits(damageFine);
		result = prime * result + (int) (temp ^ (temp >>> 32));
		temp = Double.doubleToLongBits(delayFine);
		result = prime * result + (int) (temp ^ (temp >>> 32));
		result = prime * result + id;
		result = prime * result
				+ ((realReturnDate == null) ? 0 : realReturnDate.hashCode());
		result = prime * result + ((user == null) ? 0 : user.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		IncomeRecord other = (IncomeRecord) obj;
		if (book == null) {
			if (other.book != null)
				return false;
		} else if (!book.equals(other.book))
			return false;
		if (Double.doubleToLongBits(damageFine) != Double
				.doubleToLongBits(other.damageFine))
			return false;
		if (Double.doubleToLongBits(delayFine) != Double
				.doubleToLongBits(other.delayFine))
			return false;
		if (id != other.id)
			return false;
		if (realReturnDate == null) {
			if (other.realReturnDate != null)
				return false;
		} else if (!realReturnDate.equals(other.realReturnDate))
			return false;
		if (user == null) {
			if (other.user != null)
				return false;
		} else if (!user.equals(other.user))
			return false;
		return true;
	}

}
